package hr.controller;

import hr.entity.Department;
import hr.entity.Position;
import hr.entity.vo.*;
import hr.entity.vo.request.AddNewDepartmentVo;
import hr.service.DepartmentsService;
import hr.service.EmployeesService;
import hr.utils.Result.R;
import hr.utils.Result.Result;
import hr.utils.Result.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门信息表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-12-04
 */
@Tag(name = "部门管理")
@RestController
@Slf4j
@RequestMapping("/departments")
public class DepartmentsController {
    @Autowired
    private DepartmentsService departmentsService;
    @Autowired
    private EmployeesService employeesService;

    @Operation(summary = "获取部门列表")
    @PostMapping("/getAllDepartments")
    public Result<Object> getAllDepartments(@RequestBody ObtainDepartment obtainDepartment) throws Exception {

        List<?> list = departmentsService.selectAllDepartments(obtainDepartment);
        HashMap<String, Object> map = new HashMap<>();
        map.put("allDepartmentsList", list);

        // 分页信息
        obtainDepartment = departmentsService.getPagingInformation(obtainDepartment);
        map.put("obtainDepartment", obtainDepartment);

        return ResultUtil.success(map);
    }

    @Operation(summary = "删除部门")
    @PostMapping("/delete")
    public Result<Object> deleteDepartments(@RequestBody Map<String, Integer> param) throws Exception {
        Integer departmentId = param.get("departmentId");
        Department department = departmentsService.deleteDepartmentById(departmentId);
        return department == null ? ResultUtil.error() : ResultUtil.success();
    }

    @Operation(summary = "新增部门")
    @PostMapping("/addNewDepartment")
    public Result<Object> addNewDepartment(@RequestBody AddNewDepartmentVo vo) throws Exception {
        Department department = departmentsService.addNewDepartment(vo);
        return department == null ? ResultUtil.error() : ResultUtil.success();
    }

    /**
     * 获取部门层级关系图
     *
     * @return
     * @Autor : liujiji
     */
    @PostMapping("/getDepartmentRelationshipMap")
    public Result<?> getDepartmentRelationshipMap() {
        List<?> list = departmentsService.getDepartmentRelationshipMap();
        return list == null ? ResultUtil.error() : ResultUtil.success(list);
    }

    /**
     * 获取所有可选负责人
     */
    @PostMapping("/getDepartmentHead")
    public Result<?> getDepartmentHead() {
        List<?> list = departmentsService.getDepartmentHead();
        return list == null ? ResultUtil.error() : ResultUtil.success(list);
    }

    /**
     * 修改部门
     */
    @PostMapping("/updateDepartment")
    public Result<?> updateDepartment(@RequestBody() UpdateDepartmentVo vo) throws Exception {
        Department upResDep = departmentsService.updateDepartment(vo);
        return upResDep == null ? ResultUtil.error() : ResultUtil.success(upResDep);
    }

    /**
     * 获取部门下的所有岗位
     */
    @PostMapping("/obtainJobPositionsByDepartmentId")
    public Result<?> updateDepartment(@RequestBody() ObtainJobPositionsByDepartmentIdVo vo) throws Exception {
        // 确保下面两行代码顺序不能颠倒！！！先处理分页，提前捕获异常
        Paging paging = departmentsService.handleUpdateDepartmentPage(vo);
        List<Position> list = departmentsService.obtainJobPositionsByDepartmentId(vo);

        HashMap<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("paging", paging);
        return list == null ? ResultUtil.error() : ResultUtil.success(map);
    }

    @Operation(summary = "查询各个部门的岗位数、人员数目（报表）")
    @PostMapping("/getDepartmentsPositionAndPeople")
    public R getDepartmentsPositionAndPeople() {
        Map<String,Object> map = departmentsService.handleDepartmentsPositionAndPeople();
        return R.success(map);
    }
}
